package com.gzyj.flink.gps.broadcast;

import com.gzyj.flink.driver.Driverlogininfo;
import com.gzyj.flink.gp.zdaq.TCmd020064;
import com.gzyj.flink.gps.GPSPosition;
import com.gzyj.flink.gps.addtionalfnfo.JTT808_AddtionalInfo;
import com.gzyj.flink.gps.addtionalfnfo.subiao.JTT808_0x0200_0x64;
import com.gzyj.flink.gps.addtionalfnfo.subiao.JTT808_0x0200_0x67;
import com.gzyj.flink.gps.addtionalfnfo.subiao.JTT808_0x0200_0x70;
import com.gzyj.flink.utils.PointUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

import java.util.Date;
import java.util.UUID;

/**
 * @author lhj
 */
public class TCmd020064Process extends BroadcastProcessFunction<GPSPosition, Driverlogininfo, TCmd020064> {

    private MapStateDescriptor<String, Driverlogininfo> driverlogininfoMapStateDescriptor;


    public TCmd020064Process(MapStateDescriptor<String, Driverlogininfo> driverlogininfoMapStateDescriptor) {
        this.driverlogininfoMapStateDescriptor = driverlogininfoMapStateDescriptor;
    }

    @Override
    public void processElement(GPSPosition gpsPosition, BroadcastProcessFunction<GPSPosition, Driverlogininfo, TCmd020064>.ReadOnlyContext readOnlyContext, Collector<TCmd020064> collector) throws Exception {

        ReadOnlyBroadcastState<String, Driverlogininfo> readOnlyBcState = readOnlyContext.getBroadcastState(driverlogininfoMapStateDescriptor);

        Driverlogininfo driverlogininfo = readOnlyBcState.get(gpsPosition.getVehId());

        if (gpsPosition.getVersion().equalsIgnoreCase("2019")) {
            //粤标

        } else {
            // 苏标
            for (JTT808_AddtionalInfo addtionalInfo : gpsPosition.getGnssDataAdditionalInfo()) {

                if (addtionalInfo.getID() == 0x64) {
                    JTT808_0x0200_0x64 jtt808_0x0200_0x64 = (JTT808_0x0200_0x64) addtionalInfo;
                    String platformId = gpsPosition.getJtt808_0x64_platformId();
                    String flagStateStr = jtt808_0x0200_0x64.FlagState == 0 ? "报警开始" : jtt808_0x0200_0x64.FlagState == 1 ? "报警开始" : "报警结束";
                    String DeviateTypeStr = jtt808_0x0200_0x64.DeviateType == 0x01 ? "左侧偏离" : "右侧偏离";
                    String roadSignIdentificationTypeStr = "不可用";
                    if (jtt808_0x0200_0x64.RoadSignIdentificationType == 0x01) {
                        roadSignIdentificationTypeStr = "限速标志";
                    } else if (jtt808_0x0200_0x64.RoadSignIdentificationType == 0x02) {
                        roadSignIdentificationTypeStr = "限高标志";
                    } else if (jtt808_0x0200_0x64.RoadSignIdentificationType == 0x03) {
                        roadSignIdentificationTypeStr = "限重标志";
                    }
                    String alarmLevel = jtt808_0x0200_0x64.AlarmLevel == 1 ? "一级报警" : "二级报警";

                    TCmd020064 tCmd020064 = new TCmd020064();
                    tCmd020064.setAlarmId(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x64.getAlarmId())));
                    tCmd020064.setFlagState(flagStateStr);
                    tCmd020064.setAlarmType(jtt808_0x0200_0x64.getAlarmOrEventTypeString());
                    tCmd020064.setAlarmlevel(alarmLevel);
                    tCmd020064.setFrontCarSpeed(Byte.toUnsignedInt(jtt808_0x0200_0x64.getSpeed()));
                    tCmd020064.setPedDistance(jtt808_0x0200_0x64.getCarOrPedestrianDistanceAhead().doubleValue());
                    tCmd020064.setDeviateType(DeviateTypeStr);
                    tCmd020064.setRoadSignType(roadSignIdentificationTypeStr);
                    tCmd020064.setRoadSignData(jtt808_0x0200_0x64.getRoadSignIdentificationData().toString());
                    tCmd020064.setVelocity(jtt808_0x0200_0x64.getSpeed() != 0 ? Byte.toUnsignedInt(jtt808_0x0200_0x64.getSpeed()) : gpsPosition
                            .getSpeedExt());
                    tCmd020064.setHigh(Double.valueOf(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x64.getAltitude()))));
                    tCmd020064.setLat(PointUtils.gps2double(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x64.getLatitude()))));
                    tCmd020064.setLon(PointUtils.gps2double(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x64.getLongitude()))));
                    tCmd020064.setGpsTime(jtt808_0x0200_0x64.getAlarmTime());
                    tCmd020064.setVehStatus("");
                    tCmd020064.setVehicleStatus(0);
                    tCmd020064.setState(Byte.toUnsignedInt(jtt808_0x0200_0x64.FlagState));
                    tCmd020064.setAlarmIdNumber(jtt808_0x0200_0x64.AlarmIdentificationStr);
                    tCmd020064.setASTerminalId(jtt808_0x0200_0x64.getAlarmIdentification().getTerminalID());
                    tCmd020064.setATime(jtt808_0x0200_0x64.getAlarmIdentification().Time);
                    tCmd020064.setASerialNumber(jtt808_0x0200_0x64.AlarmIdentification.SN.toString());
                    tCmd020064.setAAppendagNumber(jtt808_0x0200_0x64.AlarmIdentification.AttachCount.toString());
                    tCmd020064.setVehId(gpsPosition.getVehId());
                    tCmd020064.setDeptId(gpsPosition.getDeptId());
                    tCmd020064.setAlarmNumber(platformId);
                    tCmd020064.setSystime(new Date());

                    String driverNo = null;
                    String driverName = null;
                    if (driverlogininfo != null) {
                        driverNo = driverlogininfo.getDriverNo();
                        driverName=driverlogininfo.getDriverName();
                    }

                    tCmd020064.setDriverCode(driverNo);
                    tCmd020064.setDriverName(driverName);

                    collector.collect(tCmd020064);



                } else if (addtionalInfo.getID() == 0x67) {
                    JTT808_0x0200_0x67 jtt808_0x0200_0x67 = (JTT808_0x0200_0x67) addtionalInfo;
                    String platformId = gpsPosition.getJtt808_0x67_platformId();
                    String flagStateStr = jtt808_0x0200_0x67.FlagState == 0 ? "报警开始" : jtt808_0x0200_0x67.FlagState == 1 ? "报警开始" : "报警结束";
                    TCmd020064 tCmd020064 = new TCmd020064();
                    tCmd020064.setAlarmId(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x67.getAlarmId())));
                    tCmd020064.setFlagState(flagStateStr);
                    tCmd020064.setAlarmType(jtt808_0x0200_0x67.getAlarmOrEventTypeString());
                    tCmd020064.setFrontCarSpeed(Byte.toUnsignedInt(jtt808_0x0200_0x67.getSpeed()));
                    tCmd020064.setVelocity(jtt808_0x0200_0x67.getSpeed() != 0 ? Byte.toUnsignedInt(jtt808_0x0200_0x67.getSpeed()) : gpsPosition
                            .getSpeedExt());
                    tCmd020064.setHigh(Double.valueOf(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x67.getAltitude()))));
                    tCmd020064.setLat(PointUtils.gps2double(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x67.getLatitude()))));
                    tCmd020064.setLon(PointUtils.gps2double(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x67.getLongitude()))));
                    tCmd020064.setGpsTime(jtt808_0x0200_0x67.getAlarmTime());
                    tCmd020064.setVehStatus("");
                    tCmd020064.setVehicleStatus(jtt808_0x0200_0x67.getVehicleState());
                    tCmd020064.setState(Byte.toUnsignedInt(jtt808_0x0200_0x67.FlagState));
                    tCmd020064.setAlarmIdNumber(jtt808_0x0200_0x67.AlarmIdentificationStr);
                    tCmd020064.setASTerminalId(jtt808_0x0200_0x67.getAlarmIdentification().getTerminalID());
                    tCmd020064.setATime(jtt808_0x0200_0x67.getAlarmIdentification().Time);
                    tCmd020064.setASerialNumber(jtt808_0x0200_0x67.AlarmIdentification.SN.toString());
                    tCmd020064.setAAppendagNumber(jtt808_0x0200_0x67.AlarmIdentification.AttachCount.toString());
                    tCmd020064.setVehId(gpsPosition.getVehId());
                    tCmd020064.setDeptId(gpsPosition.getDeptId());
                    tCmd020064.setAlarmNumber(platformId);
                    tCmd020064.setSystime(new Date());

                    String driverNo = null;
                    String driverName = null;
                    if (driverlogininfo != null) {
                        driverNo = driverlogininfo.getDriverNo();
                        driverName = driverlogininfo.getDriverName();

                    }

                    tCmd020064.setDriverCode(driverNo);
                    tCmd020064.setDriverName(driverName);

                    collector.collect(tCmd020064);



                } else if (addtionalInfo.getID() == 0x70) {
                    JTT808_0x0200_0x70 jtt808_0x0200_0x70 = (JTT808_0x0200_0x70) addtionalInfo;
                    String platformId = gpsPosition.getJtt808_0x70_platformId();
                    String flagStateStr = jtt808_0x0200_0x70.FlagState == 0 ? "报警开始" : jtt808_0x0200_0x70.FlagState == 1 ? "报警开始" : "报警结束";
                    TCmd020064 tCmd020064 = new TCmd020064();
                    tCmd020064.setAlarmId(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x70.getAlarmId())));
                    tCmd020064.setFlagState(flagStateStr);
                    tCmd020064.setAlarmType(jtt808_0x0200_0x70.getAlarmOrEventTypeString());
                    tCmd020064.setVelocity(jtt808_0x0200_0x70.getSpeed() != 0 ? Byte.toUnsignedInt(jtt808_0x0200_0x70.getSpeed()) : gpsPosition
                            .getSpeedExt());
                    tCmd020064.setHigh(Double.valueOf(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x70.getAltitude()))));
                    tCmd020064.setLat(PointUtils.gps2double(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x70.getLatitude()))));
                    tCmd020064.setLon(PointUtils.gps2double(Integer.parseUnsignedInt(String.valueOf(jtt808_0x0200_0x70.getLongitude()))));
                    tCmd020064.setGpsTime(jtt808_0x0200_0x70.getAlarmTime());
                    tCmd020064.setVehStatus("");
                    tCmd020064.setVehicleStatus(0);
                    tCmd020064.setState(Byte.toUnsignedInt(jtt808_0x0200_0x70.FlagState));
                    tCmd020064.setASTerminalId(jtt808_0x0200_0x70.getAlarmIdentification().getTerminalID());
                    tCmd020064.setATime(jtt808_0x0200_0x70.getAlarmIdentification().Time);
                    tCmd020064.setASerialNumber(jtt808_0x0200_0x70.AlarmIdentification.SN.toString());
                    tCmd020064.setAAppendagNumber(jtt808_0x0200_0x70.AlarmIdentification.AttachCount.toString());
                    tCmd020064.setVehId(gpsPosition.getVehId());
                    tCmd020064.setDeptId(gpsPosition.getDeptId());
                    tCmd020064.setAlarmNumber(platformId);
                    tCmd020064.setSystime(new Date());

                    String driverNo = null;
                    String driverName = null;
                    if (driverlogininfo != null) {
                        driverNo = driverlogininfo.getDriverNo();
                        driverName=driverlogininfo.getDriverName();
                    }

                    tCmd020064.setDriverCode(driverNo);
                    tCmd020064.setDriverName(driverName);

                    collector.collect(tCmd020064);

                }


            }


        }


    }

    @Override
    public void processBroadcastElement(Driverlogininfo driverlogininfo, BroadcastProcessFunction<GPSPosition, Driverlogininfo, TCmd020064>.Context context, Collector<TCmd020064> collector) throws Exception {
        BroadcastState<String, Driverlogininfo> bcState = context.getBroadcastState(driverlogininfoMapStateDescriptor);
        bcState.put(driverlogininfo.getVehId(), driverlogininfo);
    }
}
